#!/bin/bash

set -ex

SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"

cd "$SCRIPT_DIR" || exit 1

pushd .. >/dev/null

if ! which protoc-gen-go-grpc &>/dev/null; then
	echo "protoc-gen-go-grpc is missing. Make sure to install it and add it to your PATH."
	exit 1
fi

if ! [[ -d "$SCRIPT_DIR/github.com/bentoml/bentoml/grpc" ]]; then
	protoc -I. -I ./thirdparty/protobuf/src \
		--go_out "$SCRIPT_DIR" --go_opt paths=import \
		--go-grpc_out "$SCRIPT_DIR" --go-grpc_opt paths=import \
		bentoml/grpc/v1alpha1/service.proto
	protoc -I. -I ./thirdparty/protobuf/src \
		--go_out "$SCRIPT_DIR" --go_opt paths=import \
		--go-grpc_out "$SCRIPT_DIR" --go-grpc_opt paths=import \
		bentoml/grpc/v1/service.proto
fi

popd >/dev/null

# This script is a hack to test out client.go locally without using bazel.
pushd "$SCRIPT_DIR/github.com/bentoml/bentoml/grpc/v1alpha1/" >/dev/null
! [[ -f "go.mod" ]] && go mod init v1alpha1
go mod tidy
popd >/dev/null
pushd "$SCRIPT_DIR/github.com/bentoml/bentoml/grpc/v1/" >/dev/null
! [[ -f "go.mod" ]] && go mod init v1
go mod tidy
popd >/dev/null
go mod tidy

# Then run the client
go run ./client.go
